<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Report_group extends MY_Controller {

	function Report_group() {
		parent::__construct();
		$this->load->model('report_model');
		$this->load->model('report_group_model');
	}

	public function index() {
		$reports = $this->report_model->report_list();
		$groups  = $this->report_group_model->get_group_list();
		if($this->session->userdata('logged_in') && $this->session->userdata('role_id') == 1){
			$this->load->view('report_group', array( 'groups' => $groups, 'reports' => $reports));
		} else {
			$this->load->view('login');
		}
	}

	public function add() {
		$report_name  = $this->input->post('report_name');
		$group_name   = $this->input->post('group_name');
		$group_sort   = $this->input->post('group_sort');
		$row = $this->report_model->get_by_name($report_name);
	    $report_groups = $this->report_group_model->get_group_by_name($group_name);
	    if (empty($report_groups)) {
	    	$this->report_group_model->add($group_name, $row['report_id'], $group_sort);
	    } else {
	    	echo "<script>alert('already exist');window.history.go(-1);</script>";
	    }
	    redirect("/report_group");
	}

	public function delete() {
		$group_id = $this->input->get('group_id');
		$group_ids = $this->report_group_model->get_group_by_id($group_id);
	    if (!empty($group_ids)) {
	    	$this->report_group_model->delete($group_id);
	    } else {
	    	echo "<script>alert('Not exist');window.history.go(-1);</script>";
	    }
	    redirect("/report_group");
	}

	public function update() {
		$group_id     = $this->input->post('group_id');
		$report_name  = $this->input->post('report_name');
		$group_name   = $this->input->post('group_name');
		$group_sort   = $this->input->post('group_sort');
		$row = $this->report_model->get_by_name($report_name);
		
		$this->report_group_model->update($group_id, $group_name, $row['report_id'], $group_sort);
		redirect("/report_group");
	}

	
}